import React from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { Card, Popconfirm, Divider, Button, Input, Drawer, Space, Table } from 'antd';
import { connect } from 'umi';
import moment from 'moment';
import { PlusOutlined, SyncOutlined } from '@ant-design/icons';
import CouponEdit from './components/couponEdit';

const { Search } = Input;
const currControl = 'Coupon';

class ViewPage extends React.Component<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      loading: false,
      dataSource: [],
      visible: false,
      selectedRowKeys: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      payload: {},
      record: {},
    };
  }
  componentDidMount() {
    this.fetchDatas();
  }
  fetchDatas = () => {
    this.setState({ loading: true });
    const { pageIndex, pageSize, payload } = this.state;
    this.props
      .dispatch({
        type: 'Abp/getList',
        payload: { SkipCount: (pageIndex - 1) * pageSize, MaxResultCount: pageSize, ...payload },
        control: currControl,
      })
      .then(() => {
        var resp = this.props.response;
        if (resp) {
          var dataSource = resp.result?.items;
          this.setState({ loading: false, dataSource, totalCount: resp.result?.totalCount });
        }
      });
  };
  handleDelete = (id: number) => {
    this.props
      .dispatch({
        type: 'Abp/delete',
        payload: { id },
        control: currControl,
      })
      .then(() => {
        this.fetchDatas();
      });
  };
  onSelectChange = (selectedRowKeys: any) => {
    this.setState({ selectedRowKeys });
  };
  onSearch = (value: any) => {
    var payload = this.state.payload;
    payload.keywords = value;
    this.setState({ payload }, () => {
      this.fetchDatas();
    });
  };
  openModal = (record: any, layout: string) => {
    this.setState({ visible: true, disabled: true, record, layout });
  };
  swithDrawer = (visible: boolean, record = null) => {
    this.setState({ visible, record });
  };
  render() {
    const { loading, dataSource, selectedRowKeys, totalCount, pageSize } = this.state;
    const columns: any = [
      {
        title: '序号',
        width: 60,
        align: 'center',
        key: 'index',
        render: (_: any, __: any, i: any) => {
          return i + 1;
        },
      },
      {
        title: '优惠券名称',
        width: 200,
        align: 'center',
        dataIndex: 'title',
        ellipsis: true,
      },
      {
        title: '优惠券类型',
        dataIndex: 'type',
        width: 120,
        align: 'center',
      },
      {
        title: '优惠券说明',
        dataIndex: 'memo',
        width: 180,
        align: 'center',
        ellipsis: true,
      },
      {
        title: '开始时间',
        width: 190,
        align: 'center',
        dataIndex: 'startTime',
        render: (text: string) => (
          <span>{text && text !== '' ? moment(text).format('YYYY-MM-DD HH:MM:SS') : ''}</span>
        ),
      },
      {
        title: '有效期',
        width: 190,
        align: 'center',
        dataIndex: 'endTime',
        render: (text: string) => (
          <span>{text && text !== '' ? moment(text).format('YYYY-MM-DD HH:MM:SS') : ''}</span>
        ),
      },
      {
        title: '操作',
        render: (record: any) =>
          dataSource.length >= 1 ? (
            <span>
              <Popconfirm title="确定删除?" onConfirm={() => this.handleDelete(record.id)}>
                <a>删除</a>
              </Popconfirm>
              <Divider type="vertical" />
              <a onClick={() => this.swithDrawer(true, record)}>编辑</a>
            </span>
          ) : null,
      },
    ];
    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange,
    };
    return (
      <PageHeaderWrapper title={false}>
        <Card>
          <Space>
            <Button
              type="primary"
              onClick={this.fetchDatas}
              loading={loading}
              icon={<SyncOutlined />}
            >
              刷新
            </Button>
            <Button type="primary" icon={<PlusOutlined />} onClick={() => this.swithDrawer(true)}>
              新增优惠券
            </Button>
            <Search
              style={{ width: 240 }}
              placeholder="输入关键词搜索"
              allowClear
              onSearch={(value) => this.onSearch(value)}
            />
          </Space>
        </Card>
        <Table
          rowKey="id"
          loading={loading}
          rowSelection={rowSelection}
          columns={columns}
          dataSource={dataSource}
          pagination={{
            onChange: (page) => {
              this.setState({ pageIndex: page }, () => {
                this.fetchDatas();
              });
            },
            pageSize,
            total: totalCount,
          }}
        />
        <Drawer
          width={800}
          placement="right"
          closable={false}
          onClose={() => this.swithDrawer(false)}
          visible={this.state.visible}
          destroyOnClose={true}
        >
          <CouponEdit ParentData={this.state.record} fetchDatas={this.fetchDatas} />
        </Drawer>
      </PageHeaderWrapper>
    );
  }
}
export default connect(({ Abp }: API.IConnectState) => ({
  response: Abp.response,
  currentUser: Abp.currentUser,
}))(ViewPage);
